using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SimplIDE
{
    class BuildFunctions
    {
        static public bool Build(Options op, TabControl tb, ListBox listBox1, FileTree tree)
        {
            tree.SaveAll(tb);
            string file;
            Random r = new Random();
            file = "temp" + r.Next().ToString() + ".bat";
            StreamWriter sw = new StreamWriter(file);
            FileInfo fi = new FileInfo(Application.ExecutablePath);
            sw.Write('"' + fi.DirectoryName + @"\MinGW\bin\mingw32-c++.exe"" -c ");
            for (int i = 0; i < 7; ++i)
            {
                if (i == 5 || i == 6) continue;
                CheckBox cb = (CheckBox)op.panel1.Controls[i];
                if (cb.Checked)
                {
                    sw.Write(" " + cb.Text);
                    switch (i)
                    {
                        case 4: sw.Write(" " + op.textBox1.Text); break;
                        case 5: sw.Write(" " + op.textBox2.Text); break;
                        case 6: sw.Write(" " + op.textBox3.Text); break;
                    }
                }
            }
            if (op.textBox4.Text != "") sw.Write(" " + op.textBox4.Text);
            int j = 0;
            foreach (string str in tree.FileList)
            {
                if (!Path.GetExtension(str).Equals(".cpp", StringComparison.OrdinalIgnoreCase))
                  continue;
                string objFile = Path.ChangeExtension(str, ".o");
                //objFile = Path.Combine(tree.DirPath, str);
                if (!File.Exists(objFile))
                {
                    sw.Write(" " + '"' + Path.Combine(tree.DirPath, str) + '"');
                    ++j;
                }
                else
                {
                    FileInfo fi2 = new FileInfo(objFile);
                    FileInfo fi4 = new FileInfo(Path.Combine(tree.DirPath, str));
                    if (fi2.LastWriteTimeUtc < fi4.LastWriteTimeUtc)
                    {
                        sw.Write(" " + '"' + tree.DirPath + @"\" + str + '"');
                        ++j;
                    }
                }
            }
            string file2 = "temp-" + r.Next().ToString() + ".txt";
            sw.WriteLine(" 2>" + file2);
            sw.Close();
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            if (j == 0)
            {
                MessageBox.Show("Build already done before. Now trying to link.");
            }
            else
            {
                proc.EnableRaisingEvents = false;
                proc.StartInfo.FileName = file;
                proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                proc.Start();
                proc.WaitForExit();
            }
            FileInfo fi3 = new FileInfo(file);
            fi3.Delete();
            if (j != 0 && proc.ExitCode != 0)
            {
                j = 0;
                MessageBox.Show("Build failed");
            }
            else
            {
                file = "temp" + r.Next().ToString() + ".bat";
                sw = new StreamWriter(file);
                sw.Write('"' + fi.DirectoryName + @"\MinGW\bin\mingw32-c++.exe"" ");
                for (int i = 0; i < 7; ++i)
                {
                    if (i == 5) continue;
                    CheckBox cb = (CheckBox)op.panel1.Controls[i];
                    if (cb.Checked)
                    {
                        sw.Write(" " + cb.Text);
                        switch (i)
                        {
                            case 4: sw.Write(" " + op.textBox1.Text); break;
                            case 5: sw.Write(" " + op.textBox2.Text); break;
                            case 6: sw.Write(" " + op.textBox3.Text); break;
                        }
                    }
                }
                if (op.textBox4.Text != "") sw.Write(" " + op.textBox4.Text);
                foreach (string str in tree.FileList)
                {
                  if (!Path.GetExtension(str).Equals(".cpp", StringComparison.OrdinalIgnoreCase))
                    continue;
                  string objFile = Path.ChangeExtension(str, ".o");
                  //objFile = Path.Combine(tree.DirPath, str);
                  sw.Write(" " + '"' + objFile + '"');
                }
                sw.WriteLine(" 2>>" + file2);
                sw.Close();

                proc = new System.Diagnostics.Process();
                proc.EnableRaisingEvents = false;
                proc.StartInfo.FileName = file;
                proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                proc.Start();
                proc.WaitForExit();
                fi3 = new FileInfo(file);
                fi3.Delete();
                if (proc.ExitCode != 0)
                {
                    MessageBox.Show("Linking failed");
                    j = 0;
                }
                else
                {
                    MessageBox.Show("Build succeeded");
                    j = 1;
                }
            }
            StreamReader sr = new StreamReader(file2);
            string sor;
            listBox1.Items.Clear();
            while ((sor = sr.ReadLine()) != null)
            {
                listBox1.Items.Add(sor);
            }
            sr.Close();
            fi3 = new FileInfo(file2);
            fi3.Delete();
            return j != 0;
        }

        static public void BuildAll(Options op, TabControl tb, ListBox listBox1, FileTree tree)
        {
            tree.SaveAll(tb);
            string file;
            Random r = new Random();
            file = "temp" + r.Next().ToString() + ".bat";
            StreamWriter sw = new StreamWriter(file);
            FileInfo fi = new FileInfo(Application.ExecutablePath);
            sw.Write('"' + fi.DirectoryName + @"\MinGW\bin\mingw32-c++.exe"" ");
            for (int i = 0; i < 7; ++i)
            {
                CheckBox cb = (CheckBox)op.panel1.Controls[i];
                if (cb.Checked)
                {
                    sw.Write(" " + cb.Text);
                    switch (i)
                    {
                        case 4: sw.Write(" " + op.textBox1.Text); break;
                        // case 5: sw.Write(" " + op.textBox2.Text); break;
                        case 6: sw.Write(" " + op.textBox3.Text); break;
                    }
                }
            }
            if (op.textBox4.Text != "") sw.Write(" " + op.textBox4.Text);
            foreach (string str in tree.FileList)
            {
                sw.Write(" " + '"' + tree.DirPath + @"\" + str + '"');
            }
            string file2 = "temp-" + r.Next().ToString() + ".txt";
            sw.WriteLine(" 2>" + file2);
            sw.Close();

            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = file;
            proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            proc.Start();
            proc.WaitForExit();
            if (proc.ExitCode == 0)
            {
                MessageBox.Show("Build succeeded");
            }
            else
            {
                MessageBox.Show("Build failed");
            }
            StreamReader sr = new StreamReader(file2);
            string sor;
            listBox1.Items.Clear();
            while ((sor = sr.ReadLine()) != null)
            {
                listBox1.Items.Add(sor);
            }
            sr.Close();
            fi = new FileInfo(file);
            fi.Delete();
            fi = new FileInfo(file2);
            fi.Delete();
        }

    };
}